package com.dqp.cslggroup.Api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.ConnectSever.SeverIP;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.Result;
import com.dqp.cslggroup.Util.SharedPreferencesUtil;
import com.dqp.cslggroup.Util.StringUtil;
import com.dqp.cslggroup.bean.electricity;
import okhttp3.*;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;

public class schoolApi {
    private static Connection.Response response;
    static String ip = SharedPreferencesUtil.getIp(MyApplication.getContext());

    /*
    * @Description: 获取宿舍ip
    * @Param:
    * @return:
    * @Author: 董琴平
    * @Date: 2021/9/26
    */
    public static JSONObject getIp(String xq, String ssl, String roomid, String bedid) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/school/ip")
                    .data("xq", xq)
                    .data("ssl", ssl)
                    .data("roomid", roomid)
                    .data("bedid", bedid)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.body() == null) return null;
            String json = response.body();
            if (!StringUtil.isJson(json)) return null;
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
    * @Description: 获取电量信息
    * @Param:
    * @return:
    * @Author: 董琴平
    * @Date: 2021/9/26
    */
    public static electricity getElectricity(String xq, String donS, String sshS) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("xq", xq)
                    .addFormDataPart("ds", donS)
                    .addFormDataPart("ssh", sshS)
                    .build();
            Request request = new Request.Builder()
                    .url(ip + "/cslg/school/eBill")
                    .method("POST", body)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.body() == null) return null;
            String json = response.body().string();
            if (!StringUtil.isJson(json)) return null;
            JSONObject jsonObject = JSON.parseObject(json);
            electricity electricity = new electricity();
            electricity.setDescription(jsonObject.getString("description"));
            electricity.setReturncode(jsonObject.getString("returncode"));
            electricity.setReturnmsg(jsonObject.getString("returnmsg"));
            electricity.setQuantity(jsonObject.getString("quantity"));
            electricity.setCanbuy(jsonObject.getString("canbuy"));
            electricity.setQuantityunit(jsonObject.getString("quantityunit"));
            return electricity;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /*
    * @Description: 获取打卡信息
    * @Param:
    * @return:
    * @Author: 董琴平
    * @Date: 2021/9/26
    */
    public static JSONObject getDk(String userId) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/school/DKQuery")
                    .data("userid", userId)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.statusCode()!=200) return null;
            if (response.body() == null) return null;
            String json = response.body();
            if (!StringUtil.isJson(json)) return null;
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * @Description: 获取userid
    * @Param:
    * @return:
    * @Author: 董琴平
    * @Date: 2021/9/26
    */
    public static JSONObject getUserId(String sno) {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/school/getUserid")
                    .data("sno", sno)
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.statusCode()!=200) return null;
            if (response.body() == null) return null;
            String json = response.body();
            if (!StringUtil.isJson(json)) return null;
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * @Description: 获取校历信息
     * @Param:
     * @return:
     * @Author: 董琴平
     * @Date: 2021/9/3
     */
    public static JSONObject schoolCalendar() {
        try {
            if (ip == null) ip = SeverIP.getDefvalueUrl();
            response = Jsoup.connect(ip + "/cslg/school/calendar")
                    .ignoreContentType(true)
                    .method(Connection.Method.GET)
                    .timeout(60000)
                    .execute();
            if (response.statusCode()!=200) return null;
            if (response.body() == null) return null;
            String json = response.body();
            if (!StringUtil.isJson(json)) return null;
            return JSON.parseObject(json);
        } catch (IOException e) {
            e.printStackTrace();
            Result.error("数据获取异常");
        }

        return Result.error("数据获取失败");
    }


}
